<template>
    <!--药房发药-->
    <el-container>
        <el-header>
            <el-form :inline="true" ref="form1"  class="demo-form-inline" status-icon :rules="rules" :model="form" size="mini">
                <el-col :span="8">
                    <el-form-item label="病历号" prop="caseNum">
                        <el-input  placeholder="请输入病人病历号" v-model="form.caseNum"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="时间">
                        <el-date-picker
                                type="date"
                                v-model="form.date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('form1')">查询</el-button>
                </el-form-item>
            </el-form>
        </el-header>
        <el-main>
            <el-row style="background-color: #EAF1F5">
                <el-col :span="24">
                    <el-button type="primary"  class="el-icon-success" @click="dispensing" >退药</el-button>
                </el-col>
            </el-row>
            <el-table
                    ref="multipleTable"
                    :data="tableData1"
                    height="250"
                    border
                    style="width: 100%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        :selectable="selecTable"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="drugs.drugsName"
                        label="药品名称"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="drugs.drugsPrice"
                        label="单价"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="amount"
                        sortable
                        label="数量">
                </el-table-column>
                <el-table-column
                        sortable
                        prop="state"
                        label="药品状态">
                    <template scope="scope">
                        <!--调用过滤器显示需要的数据 -->
                        <el-tag v-if="scope.row.state==4" type="primary">{{scope.row.state|mState}}</el-tag>
                        <el-tag v-else-if="scope.row.state==5" type="success">{{scope.row.state|mState}}</el-tag>
                        <el-tag v-else type="info">{{scope.row.state|mState}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="prescription.user.realName"
                        sortable
                        label="开立医生">
                </el-table-column>
                <el-table-column
                        prop="prescription.prescriptionName"
                        sortable
                        label="处方名称"
                >
                </el-table-column>
                <el-table-column
                        prop="prescription.prescriptionTime"
                        label="开立时间"
                        :formatter="dateFormat"
                        sortable
                >
                </el-table-column>
            </el-table>
        </el-main>
    </el-container>
</template>

<script>
    import {HOST} from"../../common/config"
    import {makeDate} from "../../common/dealDate";

    export default {
        name: "medicineRturn",
        data(){
            return{
                value1:'',
                tableData1:[],
                multipleSelection:[],
                form:{
                    caseNum:'',
                    date:'',
                },
                rules:{
                    caseNum:[
                        {required:true,message:'请输入病例号',trigger:'change'}
                    ],

                },
                ids:[],

            }
        },

        methods:{
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            getData(){
                console.log(this.form)
                let path = `${HOST}/drug/list`;
                this.$ajax.post(path,this.form).then((res)=>{
                    console.log(res)
                    this.tableData1=res.data
                    console.log(this.tableData1)
                })
            },
            submitForm(form1){
                this.$refs[form1].validate((valid)=>{
                    console.log(valid)
                    if(valid){
                        this.getData()
                    }else {
                        this.$message({
                            type:"error",
                            message:"查询失败"
                        })
                        return false;
                    }
                })
            },
            dateFormat(row,column,val){
                console.log(val)
                return makeDate(val)
            },
            selecTable(row, index){
                let result =false;
                if(row.state==4)
                {
                    result=true;
                }
                return result;
            },
            dispensing(){
                if(this.multipleSelection.length===0) this.$message({
                    message:"您未选择药品！",
                    type:"warning"
                })
                else{
                    let path = `${HOST}/drug/setStateBatchToReturned`
                    let param={
                        ids:[]
                    }
                    this.ids = this.multipleSelection.map((item)=>{
                        return item.id
                    })
                    param.ids = this.ids;
                    console.log(param.ids)
                    this.$ajax.post(path,param).then((res)=>{
                        console.log(res)
                        if(res.data.msg=='成功'){
                            this.$message({
                                message: '成功发出药品，病人可以取药了！',
                                type: 'success'
                            });
                            this.getData()
                        }else{
                            this.$message({
                                message: '发药失败！',
                                type: 'warning'
                            });
                        }

                    })
                }
            }
        },
        filters:{
            mState(val){
                let result;
                switch(val){
                    case 2:
                        result='未交费'
                        break;
                    case 3:
                        result='已缴费'
                        break;
                    case 4:
                        result='已发药'
                        break;
                    case 5:
                        result='已退药'
                        break;
                    case 6:
                        result='已退费'
                        break
                }
                return result;

            },
        }
    }
</script>

<style scoped>

</style>